package com.onehash.onebilling.entity;


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class PaymentRecord implements Serializable{
	


	private HashMap <String, ArrayList <Payment>> payments;
	private Observer paymentObserver; //for display use
	private static PaymentRecord paymentRecordInstance;
	
	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
	public static String delimiter = ",";
    public static final String PAYMENTS_OBJ_FILENAME="data\\payment.csv";
    
	private PaymentRecord () throws FileNotFoundException, IOException, ParseException{
		//load from file
		payments = new HashMap <String, ArrayList <Payment>>();
		 
		readPaymentsObjects(PAYMENTS_OBJ_FILENAME);
	}
	
	public HashMap<String, ArrayList<Payment>> getPayments() {
		return payments;
	}


	public static PaymentRecord getInstance() throws FileNotFoundException, IOException, ParseException{
        if(paymentRecordInstance==null){
        	paymentRecordInstance=new PaymentRecord();
        }
        return paymentRecordInstance;
    }
	
	
	
	/* Adds an observer UI class */
    public void addObserver(Observer UI){
    	paymentObserver = UI;
    }
    
    
	public void addPayment (String customerAcc, Date dateTime, double amount, String paymentMethod){
		Payment newPayment = new Payment(customerAcc,dateTime, amount, paymentMethod);
		String key = customerAcc +delimiter+sdf.format(dateTime).substring(0, 6) ; //use acc+month as key
		ArrayList <Payment> paymentArrList = payments.get(key);
		
		if (paymentArrList == null || paymentArrList.size()==0) {
			paymentArrList = new ArrayList <Payment> ();
			paymentArrList.add(newPayment);
			payments.put (key, paymentArrList);
		}else {
			paymentArrList.add(newPayment);
		}
		
	}

	public ArrayList <Payment> getPaymentArrListByCusomerAccMonth(String customerAcc, String month){
		String key = customerAcc +delimiter+ month; //use acc+month as key
		ArrayList <Payment> paymentArrList = payments.get(key);
		return paymentArrList;			
	}
	
	public double getPaymentByCusomerAccMonth(String customerAcc, String month){
		ArrayList <Payment> paymentArrList =getPaymentArrListByCusomerAccMonth(customerAcc, month);
		double amount = 0;
		for (Payment payment : paymentArrList) {
			amount += payment.getAmount();
		}
		
		return amount;			
	}


    public void readPaymentsObjects(String filename) throws FileNotFoundException, IOException, ParseException{
       
        FileInputStream fi = new FileInputStream(filename);
   	      
   		DataInputStream dis = new DataInputStream(fi);
   		BufferedReader br = new BufferedReader(new InputStreamReader(dis));
   		
   		String strLine = ""; 
   		while ((strLine = br.readLine()) != null)   {
   			String [] strArr = strLine.split(delimiter);
   			Date dateTime = (Date)sdf.parse(strArr[1]);
   			addPayment(strArr[0], dateTime, Double.parseDouble(strArr[2]),strArr[3]);
   		}			
   		    	
    }    
	


	public static void main(String[] args) throws Exception { //for testing
		PaymentRecord pr = PaymentRecord.getInstance();
		System.out.println(pr.getPaymentArrListByCusomerAccMonth("CA0001", "201102").size());
		System.out.println(pr.getPaymentByCusomerAccMonth("CA0001", "201102"));
	}
	/*
	public void show(){
		System.out.println ("************ Payment List **************");
		
	}
*/

}
